package com.cuixichang.minimalism.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuixichang.minimalism.core.mybatis.cnstants.BaseFieldNames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import com.cuixichang.minimalism.mapper.IPermissionMapper;
import com.cuixichang.minimalism.transfer.dto.IPermissionDTO;
import com.cuixichang.minimalism.transfer.factory.IPermissionAssembler;
import com.cuixichang.minimalism.transfer.po.IPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDateTime;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.function.Function;
import org.springframework.stereotype.Service;

/**
 * 系统权限表表服务实现类
 *
 * @author cuixichang
 * @date 2024-12-18
 */
@Service
public class IPermissionServiceImpl implements IPermissionService {

    @Autowired
    private IPermissionMapper iPermissionMapper;

    @Override
    public IPermissionDTO selectById(Long id){
        IPermission iPermission = iPermissionMapper.selectById(id);
        return IPermissionAssembler.INSTANCT.convertDto(iPermission);
    }

    @Override
    public IPermissionDTO selectOne(IPermissionDTO iPermissionDTO){
        Assert.notNull(iPermissionDTO,"参数为空,请检查");
        IPermission iPermission = IPermissionAssembler.INSTANCT.convertPo(iPermissionDTO);
        IPermission result = iPermissionMapper.selectOne(new QueryWrapper<>(iPermission));
        return IPermissionAssembler.INSTANCT.convertDto(result);
    }

    @Override
    public List<IPermissionDTO> selectByIds(List<Long> ids) {
        Assert.notEmpty(ids,"参数为空,请检查");
        List<IPermission> results = iPermissionMapper.selectBatchIds(ids);
        return results.stream().map(IPermissionAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public List<IPermissionDTO> selectList(IPermissionDTO iPermissionDTO){
        return this.selectList(iPermissionDTO, (iPermission, queryWrapper) -> {});
    }

    @Override
    public List<IPermissionDTO> selectList(IPermissionDTO iPermissionDTO, BiConsumer<IPermission, QueryWrapper<IPermission>> consumer){
        Assert.notNull(iPermissionDTO,"参数为空,请检查");
        IPermission iPermission = IPermissionAssembler.INSTANCT.convertPo(iPermissionDTO);
        QueryWrapper<IPermission> queryWrapper = new QueryWrapper<>(iPermission);
        if(Objects.nonNull(consumer)){
            consumer.accept(iPermission, queryWrapper);
        }
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        List<IPermission> results = iPermissionMapper.selectList(queryWrapper);
        return results.stream().map(IPermissionAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public Page<IPermissionDTO> selectPage(IPermissionDTO iPermissionDTO,long pageSize,long pageNo){
        return this.selectPage(iPermissionDTO, pageSize, pageNo, (iPermission, queryWrapper) -> {});
    }

    @Override
    public Page<IPermissionDTO> selectPage(IPermissionDTO iPermissionDTO,long pageSize,long pageNo, BiConsumer<IPermission, QueryWrapper<IPermission>> consumer){
        Assert.notNull(iPermissionDTO,"参数为空,请检查");
        IPermission iPermission = IPermissionAssembler.INSTANCT.convertPo(iPermissionDTO);
        QueryWrapper<IPermission> queryWrapper = new QueryWrapper<>(iPermission);
        if(Objects.nonNull(consumer)) consumer.accept(iPermission, queryWrapper);
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        Page<IPermission> page = iPermissionMapper.selectPage(new Page<>(pageNo,pageSize),queryWrapper);
        Page<IPermissionDTO> result = Page.of(page.getCurrent(),page.getSize(),page.getTotal());
        result.setRecords(page.getRecords().stream()
                .map(IPermissionAssembler.INSTANCT::convertDto).collect(Collectors.toList()));
        return result;
    }

    @Override
    public int deleteById(Long id){
        return iPermissionMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(List<Long> ids){
        return iPermissionMapper.deleteBatchIds(ids);
    }

    @Override
    public int deleteByCondition(IPermissionDTO iPermissionDTO){
        return deleteByCondition(iPermissionDTO, (iPermission, queryWrapper)-> {});
    }

    @Override
    public int deleteByCondition(IPermissionDTO iPermissionDTO, BiConsumer<IPermission, QueryWrapper<IPermission>> consumer){
        Assert.notNull(iPermissionDTO,"参数为空,请检查");
        IPermission iPermission = IPermissionAssembler.INSTANCT.convertPo(iPermissionDTO);
        QueryWrapper<IPermission> queryWrapper = new QueryWrapper<>(iPermission);
        if(Objects.nonNull(consumer)) {
            consumer.accept(iPermission, queryWrapper);
        }
        return iPermissionMapper.delete(queryWrapper);
    }

    @Override
    public IPermissionDTO insert(IPermissionDTO iPermissionDTO){
        return insertHasOption(iPermissionDTO, (IPermissionDTO) -> null);
    }

    @Override
    public IPermissionDTO insertHasOption(IPermissionDTO iPermissionDTO, Function<IPermissionDTO,IPermissionDTO> preOption){
        Assert.notNull(iPermissionDTO,"参数为空,请检查");
        iPermissionDTO.check();
        IPermission iPermission = IPermissionAssembler.INSTANCT.convertPo(iPermissionDTO);
        Long createLoginId = iPermission.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iPermission.setUpdateLoginId(createLoginId);
        LocalDateTime localDateTime = LocalDateTime.now();
        iPermission.setCreateTime(localDateTime);
        iPermission.setUpdateTime(localDateTime);
        Boolean isEnable = iPermission.getIsEnable();
        if(Objects.isNull(isEnable)) iPermission.setIsEnable(true);
        Integer sort = iPermission.getSort();
        if(Objects.isNull(sort)) iPermission.setSort(0);
        if(Objects.nonNull(preOption)){
            IPermissionDTO apply = preOption.apply(iPermissionDTO);
            if(Objects.nonNull(apply)) {
              return apply;
            }
        }
        int insert = iPermissionMapper.insert(iPermission);
        Assert.isTrue(insert>0,"插入失败");
        return IPermissionAssembler.INSTANCT.convertDto(iPermission);
    }

    @Override
    public int updateById(IPermissionDTO iPermissionDTO){
         return updateById(iPermissionDTO, (iPermission, updateWrapper) -> {});
    }

    @Override
    public int updateById(IPermissionDTO iPermissionDTO, BiConsumer<IPermission, UpdateWrapper<IPermission>> consumer){
        Assert.notNull(iPermissionDTO,"参数为空,请检查");
        Long id = iPermissionDTO.getId();
        Assert.notNull(id,"主键为空,请检查");
        IPermission iPermission = IPermissionAssembler.INSTANCT.convertPo(iPermissionDTO);
        Long createLoginId = iPermission.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iPermission.setCreateLoginId(null);
        iPermission.setUpdateLoginId(createLoginId);
        iPermission.setUpdateTime(LocalDateTime.now());
        UpdateWrapper<IPermission> updateWrapper = new UpdateWrapper<IPermission>().eq(BaseFieldNames.ID, id);
        if(Objects.nonNull(consumer)) consumer.accept(iPermission,updateWrapper);
        return iPermissionMapper.update(iPermission,updateWrapper);
    }
}
